package com.atguigu.ai.controller;

import com.atguigu.ai.constants.ChatTypeEnum;
import com.atguigu.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.Media;
import org.springframework.util.MimeType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/chat")
public class ChatController {

    private final ChatClient chatClient;

    private final ChatHistoryRepository chatHistoryRepository;

    @PostMapping(produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestParam(value = "prompt") String prompt,
                             @RequestParam(value = "chatId") String chatId,
                             @RequestParam(value = "files", required = false) List<MultipartFile> files) {

        chatHistoryRepository.save(ChatTypeEnum.chat.getValue(), chatId);

        if (files == null || files.isEmpty()) {
            return textChat(prompt, chatId);
        }

        return multiModelChat(prompt, chatId, files);


    }

    private Flux<String> multiModelChat(String prompt, String chatId, List<MultipartFile> files) {
        List<Media> medias = files.stream().map(item -> new Media(MimeType.valueOf(Objects.requireNonNull(item.getContentType())), item.getResource())).toList();

        return chatClient
                .prompt()
                .advisors(new Consumer<ChatClient.AdvisorSpec>() {
                    @Override
                    public void accept(ChatClient.AdvisorSpec advisorSpec) {
                        advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId);
                    }
                })
                .user(new Consumer<ChatClient.PromptUserSpec>() {
                    @Override
                    public void accept(ChatClient.PromptUserSpec promptUserSpec) {
                        promptUserSpec.text(prompt).media(medias.toArray(Media[]::new));
                    }
                })
                .stream()
                .content();
    }

    private Flux<String> textChat(String prompt, String chatId) {
        return chatClient
                .prompt()
                .advisors(new Consumer<ChatClient.AdvisorSpec>() {
                    @Override
                    public void accept(ChatClient.AdvisorSpec advisorSpec) {
                        advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId);
                    }
                })
                .user(prompt)
                .stream()
                .content();
    }
}
